﻿Imports Framework

Friend Class ToDoListControl
    Implements Framework.Activities.IToDoProvider

    Private mHost As Framework.Plugin.IHost

    Public Sub Start(ByVal host As Framework.Plugin.IHost)
        mHost = host
        UpdateTab()
    End Sub
    Public Sub [Stop]()
        mHost = Nothing
    End Sub

    Private Sub AddRangeThreadSafe(ByVal activities As System.Collections.Generic.List(Of Framework.Activities.ActivityBase)) Implements Framework.Activities.IToDoProvider.AddRange
        Framework.Threading.Invoke(AddressOf AddRange, activities)
    End Sub
    Private Sub AddRange(ByVal activities As System.Collections.Generic.List(Of Framework.Activities.ActivityBase))
        Try
            lvwActivities.BeginUpdate()
            For Each clsActivity As Activities.ActivityBase In activities
                UpdateActivity(clsActivity)
            Next
            lvwActivities.Sorting = SortOrder.Ascending
            lvwActivities.Sort()
            lvwActivities.EndUpdate()

            UpdateTab()
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub UpdateTab()
        If lvwActivities.Items.Count = 0 Then
            mHost.SetTabCaption(PlugIn.TabKey, "To Do")
        Else
            mHost.SetTabCaption(PlugIn.TabKey, String.Format("To Do ({0})", lvwActivities.Items.Count))
        End If
    End Sub

    Private Function GetKey(ByVal activity As Activities.ActivityBase) As String
        Return String.Format("{0}/{1}", activity.Type, activity.Id)
    End Function
    Private Sub UpdateActivity(ByVal activity As Activities.ActivityBase)
        Dim strKey As String = GetKey(activity)
        Dim clsItem As ListViewItem = Nothing
        If lvwActivities.Items.ContainsKey(strKey) Then
            clsItem = lvwActivities.Items(strKey)
        End If
        If clsItem IsNot Nothing And activity.Closed Then
            clsItem.Remove()
            Return
        End If

        If clsItem Is Nothing Then
            clsItem = lvwActivities.Items.Add(strKey, String.Empty, CStr(Nothing))
            clsItem.SubItems.Add(String.Empty)
            clsItem.SubItems.Add(String.Empty)
        End If
        Dim strImageKey As String = activity.ImageKey
        If strImageKey IsNot Nothing Then
            strImageKey = activity.Type & "/" & strImageKey
            If Not ilToDoList.Images.ContainsKey(strImageKey) Then
                ilToDoList.Images.Add(strImageKey, activity.Image)
            End If
        End If

        clsItem.Tag = activity
        clsItem.Text = activity.Timestamp.ToLocalTime.ToString("g")
        clsItem.SubItems(1).Text = activity.Type
        clsItem.SubItems(2).Text = activity.Information
        clsItem.ImageKey = strImageKey
    End Sub

    Public Sub EnsureVisible(ByVal activity As Framework.Activities.ActivityBase) Implements Framework.Activities.IToDoProvider.EnsureVisible
        Dim strKey As String = GetKey(activity)
        If lvwActivities.Items.ContainsKey(strKey) Then
            lvwActivities.Items(strKey).EnsureVisible()
        End If
    End Sub

    Private Sub OnActivityUpdatedThreadSafe(ByVal activity As Framework.Activities.ActivityBase) Implements Framework.Activities.IToDoProvider.OnActivityUpdated
        Framework.Threading.Invoke(AddressOf OnActivityUpdated, activity)
    End Sub
    Private Sub OnActivityUpdated(ByVal activity As Framework.Activities.ActivityBase)
        Try
            lvwActivities.BeginUpdate()
            UpdateActivity(activity)
            lvwActivities.Sorting = SortOrder.Ascending
            lvwActivities.Sort()
            lvwActivities.EndUpdate()
            UpdateTab()
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub Show1() Implements Framework.Activities.IToDoProvider.Show
        mHost.ShowTab(PlugIn.TabKey)
    End Sub

    Private Sub lvwActivities_DoubleClick(ByVal sender As Object, ByVal e As System.EventArgs) Handles lvwActivities.DoubleClick
        Try
            If lvwActivities.SelectedItems.Count = 1 Then
                Dim clsActivity As Activities.ActivityBase = CType(lvwActivities.SelectedItems(0).Tag, Activities.ActivityBase)
                clsActivity.ShowHandleUI(Me.FindForm)
            End If
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub

    Private Sub AddThreadSafe(ByVal activity As Framework.Activities.ActivityBase) Implements Framework.Activities.IToDoProvider.Add
        Framework.Threading.Invoke(AddressOf Add, activity)
    End Sub
    Private Sub Add(ByVal activity As Framework.Activities.ActivityBase)
        Try
            lvwActivities.BeginUpdate()
            UpdateActivity(activity)
            lvwActivities.Sorting = SortOrder.Ascending
            lvwActivities.Sort()
            lvwActivities.EndUpdate()
            UpdateTab()
        Catch ex As Exception
            Framework.Misc.HandleException(Me, ex)
        End Try
    End Sub
End Class
